<!--
 * @Description:
 * @Version:
 * @Autor: ps
 * @Date: 2022-09-08 11:43:43
 * @LastEditors: ps
 * @LastEditTime: 2022-09-13 18:22:05
-->
<template>
  <div class="lineChart">
    <div id="yearLine"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { LineChart } from "echarts/charts";
echarts.use([LineChart]);
import colorConst from "@/utils/custom-color.js";
export default {
  name: "lineChat",
  data() {
    return {
      chart: null,
    };
  },
  // mounted() {
  //   this.lineCharts(this.lineData, this.xData);
  // },
  beforeDestroy() {
    this.chartDes();
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    lineCharts(yData, xData) {
      let nameArr = [];
      let lineData = [];
      yData.map((item) => {
        let obj = {
          name: item.name,
          data: item.data,
          type: "line",
          showSymbol: false,
          lineStyle: {
            width: 2,
          },
        };
        lineData.push(obj);
      });
      const currentDate = new Date();
      const currentMonth = currentDate.getMonth(); // 当前月份-1
      lineData.forEach((element) => {
        let obj = {
          name: element.name,
          icon: "roundRect",
          itemStyle: {
            opacity: 0.6,
          },
        };
        nameArr.push(obj);
        element.data = element.data.slice(0, currentMonth + 1);
      });
      let option = {
        xAxis: {
          type: "category",
          boundaryGap: true,
          axisLine: { show: false },
          axisTick: { show: false },
          splitLine: { show: false },
          axisLabel: {
            color: "rgba(0, 0, 0, 1)",
            interval: 0, //使x轴文字显示全
            fontSize: "14",
            fontWeight: 400,
            // formatter: function (value) {
            //   // let arr = value.split("月");
            //   return arr[0];
            // },
          },
          data: xData,
        },
        grid: {
          left: "1%",
          right: "3%",
          bottom: "1%",
          top: "15%",
          containLabel: true,
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          trigger: "axis",
          valueFormatter: function (value) {
            return value;
          },
        },
        legend: {
          type: "scroll",
          width: 600,
          data: nameArr,
        },
        color: colorConst.customColorarr,
        series: lineData,
      };
      this.initChart("yearLine", option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.lineChart {
  width: 100%;
  height: 350px;
  #yearLine {
    // width: 773.5px;
    width: 100%;
    height: 350px;
  }
}
</style>
